package com.ftg.learn.chapter01;

/**
 * 学习一下二分
 *
 * @author lenovo
 * @version 1.0
 * @date 2023/5/13 14:51
 */

public class BinarySearch {
    /**
     * 假如说有一组有序的数
     * @param start 开始
     * @param end 末尾
     * @param target 目标
     * @return  true|false
     */
    public  static  boolean binarySearch(int start,int end,int target){
        if(start>end){
            throw new RuntimeException("参数有问题");
        }
        if(start==end){
            return start==target;
        }
        while(start<=end){
            int middle=end+start >>1;
            if(middle==target){
                return true;
            }else if(middle<target){
                start=middle+1;
            }else {
                end=middle-1;
            }
        }
        return false;
    }
    private void checkException(int start,int end){
        if(start>end){
            throw new RuntimeException("参数有问题");
        }
    }
    private static  boolean binarySearchReal(int strat,int end,int target){
        if(strat==end){
            return strat==target;
        }
        while (strat==end){
            int middle=end+strat>>1;
            if(middle==target){
                return true;
            }else if (middle<target){
                strat=middle+1;
            }else {
                end=middle-1;
            }
        }
        return false;
    }
    public static void main(String[] args) {
        System.out.println( binarySearch(1,10,7));
    }

}
